package com.ruoyi.system.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteCropService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.mapper.UserPostMapper;
import com.ruoyi.system.mapper.UserRoleMapper;
import com.ruoyi.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements IUserService {
    @Autowired
    private UserPostMapper userPostMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    RemoteCropService remoteCropService;
    @Override
    public Page<SysUser> selectPageVo(Page<SysUser> page, SysUser user) {
        return baseMapper.selectPageVo(page,user);
    }


    @Override
    public int saveAll(SysUser user) {
        // 新增用户信息
        int i = baseMapper.insert(user);
        if(i>0){
            // 新增用户岗位关联
            List<Long> posts = user.getPostIds();
            if (StringUtils.isNotEmpty(posts))
            {
                // 新增用户与岗位管理
                List<SysUserPost> list = new ArrayList<SysUserPost>();
                for (Long postId : posts)
                {
                    SysUserPost up = new SysUserPost();
                    up.setUserId(user.getUserId());
                    up.setPostId(postId);
                    list.add(up);
                }
                userPostMapper.batchUserPost(list);
            }
            // 新增用户与角色管理
            Long[] roleIds = user.getRoleIds();
            if (StringUtils.isNotEmpty(roleIds))
            {
                // 新增用户与岗位管理
                List<SysUserRole> list = new ArrayList<SysUserRole>();
                for (Long roleId : roleIds) {
                    SysUserRole ur = new SysUserRole();
                    ur.setUserId(user.getUserId());
                    ur.setRoleId(roleId);
                    list.add(ur);
                }
                userRoleMapper.batchUserRole(list);
            }
        }
        remoteCropService.FeignWarehouseById(user.getUserId(),user.getWarehouseIds());
        return i;
    }

    @Override
    public int updateUser(SysUser user) {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.delete(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, userId));
        // 新增用户与角色管理
        Long[] roleIds = user.getRoleIds();
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
        // 删除用户与岗位关联
        userPostMapper.delete(Wrappers.lambdaQuery(SysUserPost.class).eq(SysUserPost::getUserId, userId));
        // 新增用户与岗位管理
        List<Long> posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts))
        {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            userPostMapper.batchUserPost(list);
        }
        remoteCropService.FeignWarehouseById(user.getUserId(),user.getWarehouseIds());
        return baseMapper.updateById(user);
    }

    @Override
    public void addBath(List<SysUser> cacheDataList) {
        baseMapper.addBath(cacheDataList);
    }

}
